/*
 * (#)AntUrlPathMatcher.java 1.0  2013年7月16日 GMT+08:00
 */
package com.newtouch.web.security;

import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

/**
 * @author 凌蒙
 * @version V1.0, 2013年7月16日 GMT+08:00
 * @since JDK1.6
 */
public class AntUrlPathMatcher implements UrlMatcher {
	private boolean requiresLowerCaseUrl;

	public boolean isRequiresLowerCaseUrl() {
		return requiresLowerCaseUrl;
	}

	public void setRequiresLowerCaseUrl(boolean requiresLowerCaseUrl) {
		this.requiresLowerCaseUrl = requiresLowerCaseUrl;
	}

	public PathMatcher getPathMatcher() {
		return pathMatcher;
	}

	public void setPathMatcher(PathMatcher pathMatcher) {
		this.pathMatcher = pathMatcher;
	}

	private PathMatcher pathMatcher;

	public Object compile(String path) {
		if (this.requiresLowerCaseUrl) {
			return path.toLowerCase();
		}
		return path;
	}

	public AntUrlPathMatcher() {
		this(true);
	}

	public AntUrlPathMatcher(boolean requiresLowerCaseUrl) {
		this.requiresLowerCaseUrl = true;
		this.pathMatcher = new AntPathMatcher();
		this.requiresLowerCaseUrl = requiresLowerCaseUrl;
	}

	public boolean pathMatchesUrl(Object path, String url) {
		if (("/**".equals(path)) || ("**").equals(path)) {
			return true;
		}
		return this.pathMatcher.match((String) path, url);
	}

	public String getUniversalMatchPattern() {

		return "/**";
	}

	public boolean requiresLowerCaseUrl() {
		// TODO Auto-generated method stub
		return false;
	}

	public String toString() {
		return super.getClass().getName() + "[requiresLowerCase='"
				+ this.requiresLowerCaseUrl + "']";
	}
}
